package com.shichai.zimu.application;

import android.app.Activity;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.util.DisplayMetrics;

import com.danikula.videocache.HttpProxyCacheServer;
import com.gtdev5.geetolsdk.mylibrary.initialization.GeetolSDK;
import com.shichai.zimu.base.BaseActivity;
import com.shichai.zimu.base.BaseUtils;
import com.shichai.zimu.bean.WxMessageBean;
import com.shichai.zimu.constants.AppConstans;
import com.shichai.zimu.utils.BusinessHttpUtils;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.litepal.LitePalApplication;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by cheng
 * PackageName FakeCall
 * 2018/6/11 11:26
 */

public class MyApplication extends LitePalApplication {
    private static MyApplication application;
    public static boolean DEBUG = true;
    private static List<BaseActivity> activityList;     //activity管理集合
    private static List<Activity> lists=new ArrayList<>();
    private static Context context;
    //微信api
    public static IWXAPI mWxApi;
    //微信登录后的个人信息
    public WxMessageBean wxMessageBean;

    public static int screenWidth;
    public static int screenHeight;


    @Override
    public void onCreate() {
        super.onCreate();
        application=this;
        GeetolSDK.init(this, BusinessHttpUtils.URL);
        BaseUtils.init(this);
        activityList = new ArrayList<BaseActivity>();
        context=getApplicationContext();
        //初始化微信
        //第二个参数是指你应用在微信开放平台上的AppID
        mWxApi = WXAPIFactory.createWXAPI(this, AppConstans.APPID, false);
        // 将该app注册到微信
        mWxApi.registerApp(AppConstans.APPID);

        DisplayMetrics mDisplayMetrics = getApplicationContext().getResources()
                .getDisplayMetrics();
        screenWidth = mDisplayMetrics.widthPixels;
        screenHeight = mDisplayMetrics.heightPixels;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext( base );
        MultiDex.install(this);
    }

    public static Context getContext() {
                return context;
     }

    public static MyApplication getInstance(){
        return application;
    }
    public void doForFinish(BaseActivity activity){
        activityList.remove(activity);
    }

    public static void addActivity(Activity activity) {
        lists.add(activity);
    }
    public static void clearActivity() {
        if (lists != null) {
            for (Activity activity : lists) {
                activity.finish();
            }

            lists.clear();
        }
    }

    public WxMessageBean getWxMessageBean() {
        return wxMessageBean;
    }

    public void setWxMessageBean(WxMessageBean wxMessageBean) {
        this.wxMessageBean = wxMessageBean;
    }

    //=====================================================缓存区
    private HttpProxyCacheServer proxy;

    public static HttpProxyCacheServer getProxy() {
        MyApplication app = getInstance();
        return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    }

    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer(this);
    }
}
